# @Time : 2021/10/25 15:16
# @Author : Li Kunlun
# @Description : 多线程爬虫
import chapter08.py01_spider01.blog_spider as blog_spider
import threading
import time


def single_thread():
    print("single_thread begin")
    for url in blog_spider.urls:
        blog_spider.craw(url)
    print("single_thread end")


def multi_thread():
    print("multi_thread begin")
    threads = []
    for url in blog_spider.urls:
        # 创建线程
        threads.append(threading.Thread(target=blog_spider.craw, args=(url,)))

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

    print("multi_thread end")


if __name__ == '__main__':
    start = time.time()
    single_thread()
    end = time.time()
    # single thread cost: 8.11093521118164 seconds
    print("single thread cost:", end - start, "seconds")

    start = time.time()
    multi_thread()
    end = time.time()
    # multi thread cost: 0.4513895511627197 seconds
    print("multi thread cost:", end - start, "seconds")
    pass
